-- Special Thx to 0v3rlord for the German Translations
dofile("Interface/AddOns/KnightCombo/Lang/"..GetLanguage():upper()..".lua");
dofile("Interface/AddOns/KnightCombo/Lib/LibFunc.lua");

function KCombo_OnLoad(this)
	this:RegisterEvent("VARIABLES_LOADED");
	this:RegisterEvent("CASTING_START");
	this:RegisterEvent("CASTING_DELAYED");
	this:RegisterEvent("CASTING_FAILED");
	this:RegisterEvent("CASTING_STOP");
	this:RegisterEvent("WARNING_MESSAGE");
	SaveVariables("KnightCombo_Vars");
	if(not KnightCombo_Vars)then
		KnightCombo_Vars = {};
		KnightCombo_Vars["Version"] = "v1.18";								-- Version Number
		KnightCombo_Vars["Frame"] = true;									-- Buff On/Off
		KnightCombo_Vars["BuffClear"] = false;								-- Clear Target after Buff
		KnightCombo_Vars["HolySealFrame"] = true;							-- Holy Seal Attacks On/Off
		KnightCombo_Vars["HolySeal"] = 1;									-- Holy Seal Spell Toggle (For Mana or Threaten)
		KnightCombo_Vars["LightSeal"] = 1;									-- Light Seal Spell Toggle (For Punish or Shock)
		KnightCombo_Vars["Disarm"] = true;									-- Disarmament On/Off
		KnightCombo_Vars["ContAtk"] = true;									-- Continuous Attack On/Off
		KnightCombo_Vars["ResoBuff"] = false;								-- Resolution On/Off
		KnightCombo_Vars["ResoPercent"] = 20;								-- Resolution Health Percent
		KnightCombo_Vars["HoDHBuff"] = false;								-- Resolution On/Off
		KnightCombo_Vars["HoDHPercent"] = 7;								-- Resolution Health Percent
	else
		if(not KnightCombo_Vars["Frame"])then
			KnightCombo_Vars["Frame"] = true;
		end;
		if(not KnightCombo_Vars["BuffClear"])then
			KnightCombo_Vars["BuffClear"] = false;
		end;
		if(not KnightCombo_Vars["HolySealFrame"])then
			KnightCombo_Vars["HolySealFrame"] = true;
		end;
		if(not KnightCombo_Vars["HolySeal"])then
			KnightCombo_Vars["HolySeal"] = 1;
		end;
		if(not KnightCombo_Vars["LightSeal"])then
			KnightCombo_Vars["LightSeal"] = 1;
		end;
		if(not KnightCombo_Vars["Disarm"])then
			KnightCombo_Vars["Disarm"] = true;
		end;
		if(not KnightCombo_Vars["ContAtk"])then
			KnightCombo_Vars["ContAtk"] = true;
		end;
		if(not KnightCombo_Vars["ResoBuff"])then
			KnightCombo_Vars["ResoBuff"] = false;
		end;
		if(not KnightCombo_Vars["ResoPercent"])then
			KnightCombo_Vars["ResoPercent"] = 20;
		end;
		if(not KnightCombo_Vars["HoDHBuff"])then
			KnightCombo_Vars["HoDHBuff"] = false;
		end;
		if(not KnightCombo_Vars["HoDHPercent"])then
			KnightCombo_Vars["HoDHPercent"] = 7;
		end;
	end;
	if(KnightCombo_Vars["Version"] ~= "v1.18")then
		KnightConfig:Show();
		KnightCombo_Vars["Version"] = "v1.18";
	end;
	KnightConfig_Version:SetText(KnightCombo_Vars["Version"]);
	KnightConfig_LightSeal1_LightSealText1:SetText(KC_Names["Punishment"]);
	KnightConfig_LightSeal2_LightSealText2:SetText(KC_Names["Shock"]);
	KnightConfig_LightSeal3_LightSealText3:SetText("No Finisher");
	KnightConfig_HolySeal1_HolySealText1:SetText(KC_Names["Mana Return"]);
	KnightConfig_HolySeal2_HolySealText2:SetText(KC_Names["Threaten"]);
	KnightConfig_HolySeal3_HolySealText3:SetText("Toggle Spells");
	if (AddonManager) then
		local addon = {
			name = "Knight Combo",
			version = KnightCombo_Vars.Version,
			author = "aznfire987",
			description = "Knight Combo is a combination macro that can be used for Knights to make chain skill attacks with a press of a macro button. There is also an auto buffer that will use your buff when your buff wears off.",
  	 		icon = nil,
			category = "Leveling",
			configFrame = KnightConfig, 
			slashCommands = SLASH_KBUFF2.." menu",
       		miniButton = nil,
		};
		if AddonManager.RegisterAddonTable then
			AddonManager.RegisterAddonTable(addon);
		else
			AddonManager.RegisterAddon(addon.name, addon.description, addon.icon, addon.category, addon.configFrame, addon.slashCommand, addon.miniButton, addon.version, addon.author);
		end;
	else
		DEFAULT_CHAT_FRAME:AddMessage("Knight Combo addon is loaded.",0.1,0.8,0.10);
		DEFAULT_CHAT_FRAME:AddMessage("Use macro: /script HolySealCombo(), LightSealCombo(), Disarmament(), and KStun().");
		DEFAULT_CHAT_FRAME:AddMessage("Toggle Auto Knight Buff with /knightbuff or /kb (Default on)");
		DEFAULT_CHAT_FRAME:AddMessage("For Config Menu type /kb menu or use Macro button: /script KnightConfig:Show()");
	end;
end;

function KCombo_Reset()
	KnightCombo_Vars = {};
	KnightCombo_Vars["Version"] = "v1.18";								-- Version Number
	KnightCombo_Vars["Frame"] = true;									-- Buff On/Off
	KnightCombo_Vars["BuffClear"] = false;								-- Clear Target after Buff
	KnightCombo_Vars["HolySealFrame"] = true;							-- Holy Seal Attacks On/Off
	KnightCombo_Vars["HolySeal"] = 1;									-- Holy Seal Spell Toggle (For Mana or Threaten)
	KnightCombo_Vars["LightSeal"] = 1;									-- Light Seal Spell Toggle (For Punish or Shock)
	KnightCombo_Vars["Disarm"] = true;									-- Disarmament On/Off
	KnightCombo_Vars["ContAtk"] = true;									-- Continuous Attack On/Off
	KnightCombo_Vars["ResoBuff"] = false;								-- Resolution On/Off
	KnightCombo_Vars["ResoPercent"] = 20;								-- Resolution Health Percent
	KnightCombo_Vars["HoDHBuff"] = false;								-- Resolution On/Off
	KnightCombo_Vars["HoDHPercent"] = 7;								-- Resolution Health Percent
	KnightConfig:Hide();
	KnightConfig:Show();
end;

function KC_Tank()
	if((KnightCombo_Vars["Disarm"] == true) and (KnightCombo_Vars["HolySeal"] == 1) and (KnightCombo_Vars["HolySealFrame"] == true))then
		KnightCombo_Vars["HolySeal"] = 1;
		KnightCombo_Vars["Disarm"] = false;
		KnightCombo_Vars["HolySealFrame"] = true;
		Debug("Knight Combo: Tank Mode |cffff0000OFF|r. (Disarm |cffff0000OFF|r)");
		SendWarningMsg("Knight Combo: Tank Mode |cffff0000OFF|r. (Disarm |cffff0000OFF|r)");
		--SendChatMessage("Knight Combo: Tank Mode OFF. (Disarm OFF)", "SAY");
	elseif((KnightCombo_Vars["HolySeal"] ~= 3) and (KnightCombo_Vars["Disarm"] == false))then
		KnightCombo_Vars["HolySeal"] = 3;
		KnightCombo_Vars["Disarm"] = false;
		KnightCombo_Vars["HolySealFrame"] = true;
		Debug("Knight Combo: Tank Mode |cff4CFF00ON|r.");
		SendWarningMsg("Knight Combo: Tank Mode |cff4CFF00ON|r.");
		--SendChatMessage("Knight Combo: Tank Mode ON.", "SAY");
	elseif(KnightCombo_Vars["HolySealFrame"] == true)then
		KnightCombo_Vars["HolySeal"] = 1;
		KnightCombo_Vars["Disarm"] = true;
		KnightCombo_Vars["HolySealFrame"] = false;
		Debug("Knight Combo: Tank Mode |cffff0000OFF|r. (Holy Seal |cffff0000OFF|r)");
		SendWarningMsg("Knight Combo: Tank Mode |cffff0000OFF|r. (Holy Seal |cffff0000OFF|r)");
		--SendChatMessage("Knight Combo: Tank Mode OFF. (Holy Seal OFF)", "SAY");
	elseif(KnightCombo_Vars["HolySealFrame"] == false)then
		KnightCombo_Vars["HolySeal"] = 1;
		KnightCombo_Vars["Disarm"] = true;
		KnightCombo_Vars["HolySealFrame"] = true;
		Debug("Knight Combo: Tank Mode |cffff0000OFF|r. (Holy Seal |cff4CFF00ON|r)");
		SendWarningMsg("Knight Combo: Tank Mode |cffff0000OFF|r. (Holy Seal |cff4CFF00ON|r)");
		--SendChatMessage("Knight Combo: Tank Mode OFF. (Holy Seal ON)", "SAY");
	end;
	if(KnightConfig:IsVisible())then
		KnightConfig:Hide();
		KnightConfig:Show();
	end;
end;

local KC_WarningBuff, KC_Timer, KC_Action = false, 1, false;
function KCombo_OnEvent(this, event, arg1)
--	DEFAULT_CHAT_FRAME:AddMessage(event);
	if(event == "VARIABLES_LOADED")then
		this:ClearAllAnchors();
		if(KnightCombo_Vars["Frame"] == false)then
			DEFAULT_CHAT_FRAME:AddMessage("Knight Buff is |cffff0000OFF|r.");
			--HideUIPanel(this);
		else
			DEFAULT_CHAT_FRAME:AddMessage("Knight Buff is |cff4CFF00ON|r.");
		end;
	elseif(event == "CASTING_START")then
		KC_Action = true;
	elseif(event == "CASTING_STOP" or event == "CASTING_FAILED" or event == "CASTING_DELAYED")then
		KC_Action = false;
	end;
	if((event == "WARNING_MESSAGE") and arg1 == "Need to stand when casting magic")then
		KC_WarningBuff = true;
	end;
end;
-----------------------------------------------------------------
--			Attacks and Combos			--
-----------------------------------------------------------------
function LightSealCombo()
	if(SearchDebuff("target",KC_Names["Light Seal III"]) == true)then
		if(KnightCombo_Vars["LightSeal"] == 1)then
			Punisher();
		elseif(KnightCombo_Vars["LightSeal"] == 2)then
			Shock();
		end;
	else
		HolyStrike();
	end;
end;

function Punisher()
	if ClassCheck(KC_Names["Knight"], 1) or ClassCheck(KC_Names["Knight"], 2) then
		if(CDReady(KC_Names["Punishment"]))then
			CastSpellByName(KC_Names["Punishment"]);
		else
			HolyStrike();
        end;
    else
        ClassError("Knight", 0);
    end;
end;

function Shock()
    if ClassCheck(KC_Names["Knight"], 1) then
		if(CDReady(KC_Names["Shock"]))then
			CastSpellByName(KC_Names["Shock"]);
		else
			HolyStrike();
		end;
    else
        ClassError("Knight", 1);
    end;
end;

function HolyStrike()
	if ClassCheck(KC_Names["Knight"], 1) or ClassCheck(KC_Names["Knight"], 2) then
		if((SearchDebuff("target",KC_Names["Light Seal III"]) == false) or (KnightCombo_Vars["ContAtk"] == true))then
			CastSpellByName(KC_Names["Holy Strike"]);
		end;
	else
		ClassError("Knight", 0);
	end;
end;

function Disarmament()
    if ClassCheck(KC_Names["Knight"], 1) or ClassCheck(KC_Names["Knight"], 2) then
    	if(UnitLevel("player") >= 4)then
        	if(SearchDebuff("target",KC_Names["Disarmament IV"]) == true)then
            	LightSealCombo();
        	else
            	CastSpellByName(KC_Names["Disarmament"]);
        	end;
        else
        	LevelError(4);
        end;
    else
        ClassError("Knight", 0);
    end;
end;

function HolySealCombo()
	if ClassCheck(KC_Names["Knight"], 1) then
		if((KnightCombo_Vars["HolySealFrame"] == true) and (SearchDebuff("target",KC_Names["Holy Seal III"]) == true))then
			if(KnightCombo_Vars["HolySeal"] == 1)then
				ManaReturn();
			elseif(KnightCombo_Vars["HolySeal"] == 2)then
				Threaten();
			elseif(KnightCombo_Vars["HolySeal"] == 3)then
				if((SearchBuff("player",KC_Names["Threaten"]) == true) or (UnitMana("player") < 200))then
					ManaReturn();
				else
					Threaten();
				end;
			end;
		elseif(KnightCombo_Vars["Disarm"] == true)then
			Disarmament();
		else
			LightSealCombo();
		end;
	else
		ClassError("Knight", 1);
	end;
end;

function ManaReturn()
	if(UnitLevel("player") >= 2)then
		CastSpellByName(KC_Names["Mana Return"]);
	else
		LevelError(2);
		DEFAULT_CHAT_FRAME:AddMessage("Settings changed: Holy Seal = |cffff0000OFF|r.");
		KnightCombo_Vars["HolySealFrame"] = false;
	end;
end;

function Threaten()
	if(UnitLevel("player") >= 20)then
		CastSpellByName(KC_Names["Threaten"]);
	else
		LevelError(20);
		DEFAULT_CHAT_FRAME:AddMessage("Settings changed: Holy Seal = Mana Return.");
		KnightCombo_Vars["HolySeal"] = 1;
		ManaReturn();
	end;
end;

function KStun()
	if((SearchDebuff("target",KC_Names["Charge Effect"]) == false) and (SearchDebuff("target",KC_Names["Restrained"]) == false))then
		if(UnitLevel("player") >= 8)then
			if(CDReady(KC_Names["Charge"]) and ClassCheck(KC_Names["Knight"], 1) and (UnitLevel("player") >= 18))then
				CastSpellByName(KC_Names["Charge"]);
			elseif(CDReady(KC_Names["Shield of Atonement"]))then
				CastSpellByName(KC_Names["Shield of Atonement"]);
			end;
		else
			LevelError(8);
		end;
	end;
end;
-----------------------------------------------------------------
--				Auto Buffs				--
-----------------------------------------------------------------
local checkTarget = false;
function KBuff(elapsedTime)
	if(KC_WarningBuff == true)then
		KC_Timer = KC_Timer - elapsedTime;
		if(KC_Timer <= 0)then
			KC_WarningBuff = false;
			KC_Timer = 1;
		end;
	elseif(KnightCombo_Vars["Frame"] == true)then
	    if(MountChecker() == false) and (KC_Action == false) and (SearchBuff("player",KC_Names["Hide"]) == false) and (ClassCheck(KC_Names["Knight"], 1) or ClassCheck(KC_Names["Knight"], 2))then
			if (UnitLevel("player") >= 12) and (SearchBuff("player",KC_Names["Enhanced Armor"]) == false) and (SearchBuff("player",KC_Names["Significantly Enhanced Armor"]) == false) then
				CastSpellByName(KC_Names["Enhanced Armor"]);
				checkTarget = true;
			end;
			if(ClassCheck(KC_Names["Knight"], 1)) and (UnitLevel("player") >= 2) and (SearchBuff("player",KC_Names["Holy Seal"]) == false) then
				CastSpellByName(KC_Names["Holy Seal"]);
				checkTarget = true;
			end;
		end;
		if((checkTarget) and (KnightCombo_Vars["BuffClear"] == true) and UnitIsPlayer("target"))then
			TargetUnit("nil");
			checkTarget = false;
		end;
	end;
end;

function KDeathBuff()
	if((MountChecker() == false) and (KC_Action == false) and (ClassCheck(KC_Names["Knight"], 1)))then
		if((KnightCombo_Vars["ResoBuff"] == true) and (UnitHealth("player")/UnitMaxHealth("player") <= KnightCombo_Vars["ResoPercent"]/100) and (UnitLevel("player") >= 14))then
			if((SearchBuff("player",KC_Names["Resolution"]) == false) and (CDReady(KC_Names["Resolution"])))then
				CastSpellByName(KC_Names["Resolution"]);
			end;
		end;
		if((KnightCombo_Vars["HoDHBuff"] == true) and (UnitHealth("player")/UnitMaxHealth("player") <= KnightCombo_Vars["HoDHPercent"]/100) and (UnitLevel("player") >= 42))then
			if((SearchBuff("player",KC_Names["Hall of Dead Heroes"]) == false) and (CDReady(KC_Names["Hall of Dead Heroes"])))then
				CastSpellByName(KC_Names["Hall of Dead Heroes"]);
			end;
		end;
	end;
end;
-----------------------------------------------------------------
--			Menu and Xml scripts			--
-----------------------------------------------------------------
SLASH_KBUFF1 = "/knightbuff";
SLASH_KBUFF2 = "/kb";
SlashCmdList["KBUFF"] = function(editBox, msg)
	msg = string.lower(msg);
	if(msg == "menu")then
		KnightConfig:Show();
	else
		if(msg == "reset")then
			KCombo_Reset();
			DEFAULT_CHAT_FRAME:AddMessage("Knight Buff Variables Reset.")
		elseif(KnightCombo_Vars["Frame"] == true)then
			KnightCombo_Vars["Frame"] = false;
			DEFAULT_CHAT_FRAME:AddMessage("Knight Buff is |cffff0000OFF|r.");
		else
			KnightCombo_Vars["Frame"] = true;
			DEFAULT_CHAT_FRAME:AddMessage("Knight Buff is |cff4CFF00ON|r.");
		end;
	end;
end;

function KC_Show()
	KnightConfig_EnableBuff:SetChecked((KnightCombo_Vars["Frame"] and 1) or nil);
	KnightConfig_ClearTarget:SetChecked((KnightCombo_Vars["BuffClear"] and 1) or nil);
	LightSealSet("checked");
	KnightConfig_EnableHolySeal:SetChecked((KnightCombo_Vars["HolySealFrame"] and 1) or nil);
	HolySealSet("checked");
	KnightConfig_EnableDisarm:SetChecked((KnightCombo_Vars["Disarm"] and 1) or nil);
	KnightConfig_EnableContAtk:SetChecked((KnightCombo_Vars["ContAtk"] and 1) or nil);
	KnightConfig_EnableReso:SetChecked((KnightCombo_Vars["ResoBuff"] and 1) or nil);
	KnightConfig_ResoPercent:SetText(KnightCombo_Vars["ResoPercent"]);
	KnightConfig_EnableHoDH:SetChecked((KnightCombo_Vars["HoDHBuff"] and 1) or nil);
	KnightConfig_HoDHPercent:SetText(KnightCombo_Vars["HoDHPercent"]);
end;

function KC_Save()
	KnightCombo_Vars["Frame"] = (KnightConfig_EnableBuff:IsChecked() and true) or false;
	KnightCombo_Vars["BuffClear"] = (KnightConfig_ClearTarget:IsChecked() and true) or false;
	KnightCombo_Vars["Version"] = "v1.18";
	LightSealSet("saved");
	KnightCombo_Vars["HolySealFrame"] = (KnightConfig_EnableHolySeal:IsChecked() and true) or false;
	HolySealSet("saved");
	KnightCombo_Vars["Disarm"] = (KnightConfig_EnableDisarm:IsChecked() and true) or false;
	KnightCombo_Vars["ContAtk"] = (KnightConfig_EnableContAtk:IsChecked() and true) or false;
	KnightCombo_Vars["ResoBuff"] = (KnightConfig_EnableReso:IsChecked() and true) or false;
	KnightCombo_Vars["ResoPercent"] = KnightConfig_ResoPercent:GetText();
	KnightCombo_Vars["HoDHBuff"] = (KnightConfig_EnableHoDH:IsChecked() and true) or false;
	KnightCombo_Vars["HoDHPercent"] = KnightConfig_HoDHPercent:GetText();
	if(KnightCombo_Vars["Frame"] == false)then
		DEFAULT_CHAT_FRAME:AddMessage("Knight Buff is |cffff0000OFF|r.");
	elseif(KnightCombo_Vars["Frame"] == true)then
		DEFAULT_CHAT_FRAME:AddMessage("Knight Buff is |cff4CFF00ON|r.");
	end;
	
	KnightConfig:Hide();
	KnightConfig:Show();
end;

function KC_Close()
	KnightConfig:Hide();
end;

function KC_Toggle()
	if(KnightConfig:IsVisible())then
		KnightConfig:Hide();
	else
		KnightConfig:Show();
	end;
end;

function LightSealSet(str)
	if(str == "saved")then
		KnightCombo_Vars["LightSeal"] = KCCheckRadio("LightSeal");
	elseif(str == "checked")then
		KnightConfig_LightSeal1:SetChecked(nil);
		KnightConfig_LightSeal2:SetChecked(nil);
		KnightConfig_LightSeal3:SetChecked(nil);
		if (KnightCombo_Vars["LightSeal"] == 1)then
			KnightConfig_LightSeal1:SetChecked(1);
		elseif(KnightCombo_Vars["LightSeal"] == 2)then
			KnightConfig_LightSeal2:SetChecked(1);
		elseif(KnightCombo_Vars["LightSeal"] == 3)then
			KnightConfig_LightSeal3:SetChecked(1);
		end;
	end;	
end;

function HolySealSet(str)
	if(str == "saved")then
		KnightCombo_Vars["HolySeal"] = KCCheckRadio("HolySeal");
	elseif(str == "checked")then
		KnightConfig_HolySeal1:SetChecked(nil);
		KnightConfig_HolySeal2:SetChecked(nil);
		KnightConfig_HolySeal3:SetChecked(nil);
		if (KnightCombo_Vars["HolySeal"] == 1)then
			KnightConfig_HolySeal1:SetChecked(1);
		elseif(KnightCombo_Vars["HolySeal"] == 2)then
			KnightConfig_HolySeal2:SetChecked(1);
		elseif(KnightCombo_Vars["HolySeal"] == 3)then
			KnightConfig_HolySeal3:SetChecked(1);
		end;
	end;
end;

function KCCheckRadio(str)
local i = 0;
	if(str == "LightSeal")then
		while(i <= 4)do
			i = i + 1;
			if((i == 1) and (KnightConfig_LightSeal1:IsChecked()))then
				return i;
			elseif((i == 2) and (KnightConfig_LightSeal2:IsChecked()))then
				return i;
			elseif((i == 3) and (KnightConfig_LightSeal3:IsChecked()))then
				return i;
			elseif(i > 2)then
				return 1;
			end;
		end;
	elseif(str == "HolySeal")then
		while(i <= 4)do
			i = i + 1;
			if((i == 1) and (KnightConfig_HolySeal1:IsChecked()))then
				return i;
			elseif((i == 2) and (KnightConfig_HolySeal2:IsChecked()))then
				return i;
			elseif((i == 3) and (KnightConfig_HolySeal3:IsChecked()))then
				return i;
			elseif(i > 3)then
				return 1;
			end;
		end;
		return i;
	end;
end;

function KC_enableBuff()
	KnightCombo_Vars["Frame"] = true;
end

function KC_disableBuff()
	KnightCombo_Vars["Frame"] = false;
end

function ReloadKC()
	dofile("Interface/AddOns/KnightCombo/KC.lua");
	Debug("Knight Combo Reloaded.");
end;